﻿using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Clinic.Extensions
{
    public class Skin
    {
        public Task UpdateSkin(SkinType skin)
        {
            string key = App.Configuration.AppSettings.Settings["SkinType"].Value;
            if (skin.ToString() == key)
                return Task.CompletedTask;
            App.Configuration.AppSettings.Settings["SkinType"].Value = skin.ToString();
            App.Configuration.Save();
            var skins0 = Application.Current.Resources.MergedDictionaries[0];
            skins0.MergedDictionaries.Clear();
            skins0.MergedDictionaries.Add(GetSkin(skin));

            var skins1 = Application.Current.Resources.MergedDictionaries[1];
            skins1.MergedDictionaries.Clear();
            skins1.MergedDictionaries.Add(new ResourceDictionary
            {
                Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
            });

            Application.Current.MainWindow?.OnApplyTemplate();
            return Task.CompletedTask;
        }

        public ResourceDictionary GetSkin(SkinType skin)
        {
            var uri = new Uri($"pack://application:,,,/HandyControl;component/Themes/Skin{skin}.xaml");
            return new ResourceDictionary
            {
                Source = uri
            };
        }
    }
}
